package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-02-23 17:29
 **/
public class LeetCode917 {

    public static void main(String[] args) {
        LeetCode917 c = new LeetCode917();
        System.out.println(c.reverseOnlyLetters("ab-cd"));
    }

    public String reverseOnlyLetters(String s) {
        char[] letters = s.toCharArray();
        int l = 0;
        int r = letters.length - 1;
        while (l < r) {
            while (l < r && !Character.isLetter(letters[l])) {
                ++l;
            }

            while (l < r && !Character.isLetter(letters[r])) {
                --r;
            }

            char t = letters[l];
            letters[l] = letters[r];
            letters[r] = t;
            ++l;
            --r;
        }

        return String.valueOf(letters);
    }

}
